//
//  AppDelegate.m
//  FinApp
//
//  Created by Marcel Arbó Lack on 09/11/11.
//  Copyright (c) 2011 FIB-Fàcultat d'informàtica de Barcelona. All rights reserved.
//

#import "AppDelegate.h"

#import "BuscadorViewController.h"
#import "CercaViewController.h"
#import "FavoritsViewController.h"
#import "LocationManager.h"
#import "DetailViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize tabBarController = _tabBarController;
@synthesize activityView = _activityView;
- (void)dealloc
{
    [_window release];
    [_tabBarController release];
    [_activityView release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    UIViewController *viewController1, *viewController2, *viewController3;
    UIViewController *navigationController1, *navigationController2, *navigationController3;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    
        viewController1 = [[[BuscadorViewController alloc] initWithNibName:@"BuscadorViewController" bundle:nil] autorelease];
        navigationController1 = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];
        
        viewController2 = [[[CercaViewController alloc] initWithNibName:@"CercaViewController" bundle:nil] autorelease];
        navigationController2 = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];
        
        viewController3 = [[[FavoritsViewController alloc] initWithNibName:@"FavoritsViewController" bundle:nil] autorelease];
        navigationController3 = [[[UINavigationController alloc] initWithRootViewController:viewController3] autorelease];
        
    } else {
        viewController1 = [[[BuscadorViewController alloc] initWithNibName:@"BuscadorViewController_iPad" bundle:nil] autorelease];
        viewController2 = [[[CercaViewController alloc] initWithNibName:@"CercaViewController_iPad" bundle:nil] autorelease];
        viewController3 = [[[FavoritsViewController alloc] initWithNibName:@"FavoritsViewController_iPad" bundle:nil] autorelease];
    }
    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController1, navigationController2, navigationController3, nil];
    self.window.rootViewController = self.tabBarController;
    [_tabBarController setSelectedIndex:1];
    [self.window makeKeyAndVisible];
    //Demanen la localitzacio
        [[LocationManager sharedInstance] askLocation];
    
    UILocalNotification *localNotif =
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotif) {
        NSLog(@"Recieved Notification %@",localNotif);
        DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
        [detailVC setIdFlat:[[[localNotif userInfo] objectForKey:@"idPis"] intValue]];
        [[[self.tabBarController selectedViewController] navigationController] pushViewController:detailVC animated:NO];
        [detailVC release];
    }
    
    return YES;
}

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
    // Handle the notificaton when the app is running
    NSLog(@"Recieved Notification %@",[[notif userInfo] description]);
    DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    [detailVC setHidesBottomBarWhenPushed:YES];
    [detailVC setIdFlat:[[[notif userInfo] objectForKey:@"idPis"] intValue]];
    [[self.tabBarController selectedViewController] pushViewController:detailVC animated:NO];
    [detailVC release];
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
     */
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    /*
     Called when the application is about to terminate.
     Save data if appropriate.
     See also applicationDidEnterBackground:.
     */
}

/*
// Optional UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
}
*/

/*
// Optional UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
}
*/

#pragma mark - Public Methods

- (void)setLocalizedTitlesForTabBarItems {
    //Set the Localized title for each TabBarItem
    [[[[self tabBarController] viewControllers] objectAtIndex:0] setTitle:NSLocalizedString(@"_buscador", @"")];
    [[[[self tabBarController] viewControllers] objectAtIndex:1] setTitle:NSLocalizedString(@"_cerca", @"")];
    [[[[self tabBarController] viewControllers] objectAtIndex:2] setTitle:NSLocalizedString(@"_favorits", @"")];
}

- (void)showBigActivityWithText:(NSString*)text{
    if (!self.activityView) {
        self.activityView = [[UIView alloc] initWithFrame:_window.frame];
        [self.activityView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.2]];
        
        UIImageView *bigActivity = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"comun_timer.png"]];
        [bigActivity setCenter:CGPointMake(320.0/2, 200)];
        UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [activityIndicatorView setCenter:CGPointMake(bigActivity.center.x-66, bigActivity.center.y+35)];
        [activityIndicatorView startAnimating];
        UILabel *loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
        [loadingLabel setCenter:CGPointMake(bigActivity.center.x, bigActivity.center.y+35)];
        [loadingLabel setBackgroundColor:[UIColor clearColor]];
        [loadingLabel setTextColor:[UIColor colorWithRed:26/255.0 green:58/255.0 blue:50/255.0 alpha:1]];
        [loadingLabel setTextAlignment:UITextAlignmentCenter];
        [loadingLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:13]];
        [loadingLabel setText:text];
        
        [self.activityView addSubview:bigActivity];
        [self.activityView addSubview:activityIndicatorView];
        [self.activityView addSubview:loadingLabel];
        
        [bigActivity release];
        [activityIndicatorView release];
        [loadingLabel release];
    }
    
    [self.window addSubview:self.activityView];
}

- (void)removeBigActivity {
    
    [self.activityView removeFromSuperview];
}

@end
